var configFun = function () {
    var t = this;
};
configFun.prototype = {
    data: {
        tree: [],
        treeStorage: ''
    },
    show: function (reg) {
        var t = this;
        if (t.isArray(reg)) {
            for (var i = 0; i < reg.length; i++) {
                reg[i].addClass('show');
                if (reg[i].hasClass('hide')) {
                    reg[i].removeClass('hide');
                }
            }
        } else {
            reg.addClass('show');
            if (reg.hasClass('hide')) {
                reg.removeClass('hide');
            }
        }
    },
    hide: function (reg) {
        var t = this;
        if (t.isArray(reg)) {
            for (var i = 0; i < reg.length; i++) {
                reg[i].addClass('hide');
                if (reg[i].hasClass('show')) {
                    reg[i].removeClass('show');
                }
            }
        } else {
            reg.addClass('hide');
            if (reg.hasClass('show')) {
                reg.removeClass('show');
            }
        }
    },
    isArray: function (value) {
        if (typeof Array.isArray === "function") {
            return Array.isArray(value);
        } else {
            return Object.prototype.toString.call(value) === "[object Array]";
        }
    },
    tree: function (res) {
        var t = this;
        var type = 0;
        var data = res.data;
        var wrapper = $(res.wrapper);
        var one = [];
        var next = [];
        if (typeof (res.type) == 'undefined') {
            type = 0;
        } else {
            type = res.type;
        }
        if (typeof (res.check) != 'undefined' && res.check.length > 0) {
            for (var i = 0; i < data.length; i++) {
                var id = data[i].id;
                data[i]['check'] = false;
                for (var k = 0; k < res.check.length; k++) {
                    if (id == res.check[k]) {
                        data[i]['check'] = true;
                        break;
                    }
                }
            }
        }
        for (var i = 0; i < data.length; i++) {
            if (data[i].pid == null) {
                console.log("123");
                var fatherHtml = $('<div class="f-treeListWrapper"><div class="f-treeList-line"></div></div>');
                var listHtml = '';
                if (res.type == 0) {
                    listHtml = $('<div class="f-treeList f-treeListOne">' +
                        '<div class="f-treeList-top">' +
                        '<div class="f-treeList-old"><span class="f-iconJia"></span></div>' +
                        '<div class="f-treeList-title">' +
                        '<div class="f-treeList-titleImg"><span class="f-iconBi"></span></div>' +
                        '<p class="f-treeList-titleP">' + data[i].title + '</p>' +
                        '</div></div></div>');
                }
                if (res.type == 1) {
                    var radioIconHtml = '<span class="f-iconRadio"></span>';
                    if (data[i].check) {
                        radioIconHtml = '<span class="f-iconRadioTrue"></span>';
                    }
                    listHtml = $('<div class="f-treeList f-treeListOne">' +
                        '<div class="f-treeList-top">' +
                        '<div class="f-treeList-old"><span class="f-iconJia"></span></div>' +
                        '<div class="f-treeList-title">' +
                        '<div class="f-treeList-radio">' +
                        radioIconHtml +
                        '<input value="' + data[i].id + '"/>' +
                        '</div>' +
                        '<div class="f-treeList-titleImg"><span class="f-iconBi"></span></div>' +
                        '<p class="f-treeList-titleP">' + data[i].title + '</p>' +
                        '</div></div></div>');
                }
                fatherHtml.append(listHtml);
                wrapper.append(fatherHtml);
                data[i]['list'] = listHtml;
                one.push(data[i]);
            } else {
                next.push(data[i]);
            }
        }
        for (var i = 0; i < one.length; i++) {
            if (i == one.length - 1) {
            }
            (function (k) {
                var selfList = one[k].list;
                var old = selfList.find('.f-treeList-old').eq(0);
                old[0].onclick = function () {
                    var iconDiv = old.children('span').eq(0);
                    var flagClass = iconDiv.attr('class');
                    var listWrapper = old.parents('.f-treeList-top').eq(0).siblings('.f-treeListWrapper').eq(0);
                    old.addClass('f-on');
                    if (flagClass == 'f-iconJia') {
                        t.show([listWrapper]);
                        iconDiv.removeClass(flagClass);
                        iconDiv.addClass('f-iconJian');
                    }
                    if (flagClass == 'f-iconJian') {
                        t.hide([listWrapper]);
                        iconDiv.removeClass(flagClass);
                        iconDiv.addClass('f-iconJia');
                    }
                };
            })(i);
            t.treeFor({
                type: type,
                data: next,
                dataHover: one[i]
            });
        }
    },
    treeFor: function (res) {
        var t = this;
        var data = res.data;
        var dataHover = res.dataHover;
        var fatherHtml = $('<div class="f-treeListWrapper hide"><div class="f-treeList-lineShu"></div></div>');
        var listHtml = '';
        var self = [];
        var next = [];
        for (var i = 0; i < data.length; i++) {
            if (data[i].pid == dataHover.id) {
                if (res.type == 0) {
                    listHtml = $('<div class="f-treeList">' +
                        '<div class="f-treeList-top">' +
                        '<div class="f-treeList-old"><span class="f-iconJia"></span></div>' +
                        '<div class="f-treeList-title">' +
                        '<div class="f-treeList-titleImg"><span class="f-iconBi"></span></div>' +
                        '<p class="f-treeList-titleP">' + data[i].title + '</p>' +
                        '</div></div></div>');
                }
                if (res.type == 1) {
                    var radioIconHtml = '<span class="f-iconRadio"></span>';
                    if (data[i].check) {
                        radioIconHtml = '<span class="f-iconRadioTrue"></span>';
                    }
                    listHtml = $('<div class="f-treeList">' +
                        '<div class="f-treeList-top">' +
                        '<div class="f-treeList-old"><span class="f-iconJia"></span></div>' +
                        '<div class="f-treeList-title">' +
                        '<div class="f-treeList-radio">' +
                        radioIconHtml +
                        '<input value="' + data[i].id + '"/>' +
                        '</div>' +
                        '<div class="f-treeList-titleImg"><span class="f-iconBi"></span></div>' +
                        '<p class="f-treeList-titleP">' + data[i].title + '</p>' +
                        '</div></div></div>');
                }
                fatherHtml.append(listHtml);
                dataHover.list.append(fatherHtml);
                data[i]['list'] = listHtml;
                self.push(data[i]);
            } else {
                next.push(data[i]);
            }
        }
        if (self.length > 0) {
            for (var i = 0; i < self.length; i++) {
                t.treeFor({
                    type: res.type,
                    data: next,
                    dataHover: self[i]
                });
            }
        }
        if (self.length == 0 && res.dataHover.pid != null) {
            var selfDom = res.dataHover.list;
            var selfWrapper = selfDom.parent('.f-treeListWrapper');
            var selfList = selfWrapper.parent('.f-treeList');
            var fatherOld = selfList.children('.f-treeList-top').eq(0).children('.f-treeList-old').eq(0);
            var selfNextDom = selfDom.next();
            var line = $('<div class="f-treeList-lineEnd"></div>');
            var selfDom_top = selfDom.find('.f-treeList-top').eq(0);
            var selfDom_old = selfDom.find('.f-treeList-old').eq(0);
            var selfDom_img = selfDom.find('.f-treeList-titleImg').eq(0).children('span').eq(0);
            if (typeof (selfNextDom[0]) == 'undefined') {
                var fatherLine = selfDom.siblings('.f-treeList-lineShu');
                fatherLine.removeClass(fatherLine.attr('class'));
                fatherLine.addClass('f-treeList-lineShuEnd');
            }
            selfDom_top.prepend(line);
            selfDom_old.remove();
            selfDom_img.removeClass(selfDom_img.attr('class'));
            selfDom_img.addClass('f-iconEnd');
            if (fatherOld.attr('class').indexOf('f-on') == -1) {
                fatherOld[0].onclick = function () {
                    var iconDiv = fatherOld.children('span').eq(0);
                    var flagClass = iconDiv.attr('class');
                    fatherOld.addClass('f-on');
                    if (flagClass == 'f-iconJia') {
                        t.show([selfWrapper]);
                        iconDiv.removeClass(flagClass);
                        iconDiv.addClass('f-iconJian');
                    }
                    if (flagClass == 'f-iconJian') {
                        t.hide([selfWrapper]);
                        iconDiv.removeClass(flagClass);
                        iconDiv.addClass('f-iconJia');
                    }
                };
            }
            if (res.type == 1) {
                var radio = selfDom.find('.f-treeList-radio').eq(0);
                var radio_icon = radio.children('span').eq(0);
                var radioFather = selfList.children('.f-treeList-top').eq(0).find('.f-treeList-radio').eq(0);
                var radioFather_icon = radioFather.children('span').eq(0);
                radio[0].onclick = function () {
                    var radio_father = radio.parents('.f-treeListWrapper');
                    var radio_class = radio_icon.attr('class');
                    radio_icon.removeClass(radio_icon.attr('class'));
                    if (radio_class == 'f-iconRadio') {
                        radio_icon.addClass('f-iconRadioTrue');
                        for (var i = 0; i < radio_father.length - 1; i++) {
                            var radio_father_radio = radio_father.eq(i).siblings('.f-treeList-top').eq(0).find('.f-treeList-radio').eq(0);
                            var radio_father_radioIcon = radio_father_radio.children('span').eq(0);
                            radio_father_radioIcon.removeClass(radio_father_radioIcon.attr('class'));
                            radio_father_radioIcon.addClass('f-iconRadioTrue');
                        }
                    } else {
                        radio_icon.addClass('f-iconRadio');
                        for (var i = 0; i < radio_father.length - 1; i++) {
                            var radio_father_radio = radio_father.eq(i).siblings('.f-treeList-top').eq(0).find('.f-treeList-radio').eq(0);
                            var radio_father_radioIcon = radio_father_radio.children('span').eq(0);
                            var radio_fatherList = radio_father.eq(i).children('.f-treeList');
                            var flagNum = 0;
                            for (var k = 0; k < radio_fatherList.length; k++) {
                                var radio_fatherList_radio = radio_fatherList.eq(k).children('.f-treeList-top').eq(0).find('.f-treeList-radio').eq(0);
                                var radio_fatherList_radioIcon = radio_fatherList_radio.children('span').eq(0);
                                if (radio_fatherList_radioIcon.hasClass('f-iconRadio')) {
                                    flagNum++;
                                }
                            }
                            if (flagNum == radio_fatherList.length) {
                                radio_father_radioIcon.removeClass(radio_father_radioIcon.attr('class'));
                                radio_father_radioIcon.addClass('f-iconRadio');
                            }
                        }
                    }
                };
                radioFather[0].onclick = function () {
                    var radioFather_class = radioFather_icon.attr('class');
                    var radio_next = selfWrapper.find('.f-treeList-radio');
                    var radioFather_father = radioFather;
                    var radioList = radioFather.parents('.f-treeList-title').eq(0).parents('.f-treeList-top').eq(0).parents('.f-treeList').eq(0);
                    radioFather_icon.removeClass(radioFather_icon.attr('class'));
                    if (radioFather_class == 'f-iconRadio') {
                        radioFather_icon.addClass('f-iconRadioTrue');
                        t.fatherRadio_true(radioList);
                        for (var i = 0; i < radio_next.length; i++) {
                            var redio_nextIcon = radio_next.eq(i).children('span').eq(0);
                            redio_nextIcon.removeClass(redio_nextIcon.attr('class'));
                            redio_nextIcon.addClass('f-iconRadioTrue');
                        }
                    } else {
                        radioFather_icon.addClass('f-iconRadio');
                        t.fatherRadio_false(radioList);
                        for (var i = 0; i < radio_next.length; i++) {
                            var redio_nextIcon = radio_next.eq(i).children('span').eq(0);
                            redio_nextIcon.removeClass(redio_nextIcon.attr('class'));
                            redio_nextIcon.addClass('f-iconRadio');
                        }
                    }
                }
            }
            var topDom = selfDom.parents('.f-treeListOne').eq(0);
            var oneDom = topDom.parents('.f-treeListWrapper').eq(0);
            var treelist = oneDom.find('.f-treeList');
            for (var i = 0; i < treelist.length; i++) {
                if (treelist.eq(i).siblings().length == 1) {
                    var endLsit = treelist.eq(i).children('.f-treeList-top').eq(0).children('.f-treeList-lineEnd');
                    if (endLsit.length == 0) {
                        (function (k) {
                            var hoverList = treelist.eq(k);
                            var radio = hoverList.find('.f-treeList-radio').eq(0);
                            var old = hoverList.find('.f-treeList-old').eq(0);
                            if (res.type == 1) {
                                radio[0].onclick = function () {
                                    var radio_icon = $(this).children('span').eq(0);
                                    var radio_class = radio_icon.attr('class');
                                    var fatherWrapper = $(this).parents('.f-treeListOne').eq(0);
                                    var radioZi = fatherWrapper.find('.f-treeList-radio');
                                    radio_icon.removeClass(radio_icon.attr('class'));
                                    if (radio_class == 'f-iconRadio') {
                                        for (var i = 0; i < radioZi.length; i++) {
                                            var radioZi_icon = radioZi.eq(i).children('span').eq(0);
                                            radioZi_icon.removeClass(radioZi_icon.attr('class'));
                                            radioZi_icon.addClass('f-iconRadioTrue');
                                        }
                                    } else {
                                        for (var i = 0; i < radioZi.length; i++) {
                                            var radioZi_icon = radioZi.eq(i).children('span').eq(0);
                                            radioZi_icon.removeClass(radioZi_icon.attr('class'));
                                            radioZi_icon.addClass('f-iconRadio');
                                        }
                                    }
                                };
                            }
                            old[0].onclick = function () {
                                var radio_icon = $(this).siblings('.f-treeList-title').eq(0).children('.f-treeList-radio').eq(0).children('span').eq(0);
                                var radio_class = radio_icon.attr('class');
                                var fatherWrapper = $(this).parents('.f-treeListOne').eq(0);
                                var radioZi = fatherWrapper.find('.f-treeList-radio');
                                if ($(this).attr('class').indexOf('f-on') == -1) {
                                    var fatherOld = $(this);
                                    var nextWrapper = fatherOld.parents('.f-treeList').eq(0).children('.f-treeListWrapper').eq(0);
                                    var iconDiv = fatherOld.children('span').eq(0);
                                    var flagClass = iconDiv.attr('class');
                                    if (flagClass == 'f-iconJia') {
                                        iconDiv.removeClass(flagClass);
                                        iconDiv.addClass('f-iconJian');
                                        nextWrapper.removeClass('hide');
                                        nextWrapper.addClass('show');
                                        if (radio_class == 'f-iconRadioTrue') {
                                            for (var i = 0; i < radioZi.length; i++) {
                                                var radioZi_icon = radioZi.eq(i).children('span').eq(0);
                                                radioZi_icon.removeClass(radioZi_icon.attr('class'));
                                                radioZi_icon.addClass('f-iconRadioTrue');
                                            }
                                        }
                                    }
                                    if (flagClass == 'f-iconJian') {
                                        iconDiv.removeClass(flagClass);
                                        iconDiv.addClass('f-iconJia');
                                        nextWrapper.removeClass('show');
                                        nextWrapper.addClass('hide');
                                    }
                                }
                            }
                        })(i)
                    }
                }
            }
        }
    },
    treeJson: function (res) {
        var wrapper = $(res.wrapper);
        var radio = wrapper.find('.f-treeList-radio');
        var radioArr = [];
        for (var i = 0; i < radio.length; i++) {
            var radioIcon = radio.eq(i).children('span').eq(0);
            var radioId = radio.eq(i).children('input').val();
            if (radioIcon.hasClass('f-iconRadioTrue')) {
                radioArr.push(radioId);
            }
        }
        res.success(radioArr);
    },
    fatherRadio_true: function (res) {
        var t = this;
        var fatherList = res.parents('.f-treeList').eq(0);
        var fatherList_radio = fatherList.find('.f-treeList-radio').eq(0).find('span').eq(0);
        fatherList_radio.removeClass(fatherList_radio.attr('class'));
        fatherList_radio.addClass('f-iconRadioTrue');
        if (fatherList.attr('class').indexOf('f-treeListOne') == -1) {
            t.fatherRadio_true(fatherList)
        }
    },
    fatherRadio_false: function (res) {
        var t = this;
        var fatherList = res.parents('.f-treeList').eq(0);
        var fatherList_radio = fatherList.find('.f-treeList-radio').eq(0).find('span').eq(0);
        var sibList = res.siblings('.f-treeList');
        var num = 0;
        for (var i = 0; i < sibList.length; i++) {
            var sibList_radio = sibList.eq(i).find('.f-treeList-radio').eq(0).find('span').eq(0);
            if (sibList_radio.attr('class') == 'f-iconRadio') {
                num++;
            }
        }
        if (num == sibList.length) {
            fatherList_radio.removeClass(fatherList_radio.attr('class'));
            fatherList_radio.addClass('f-iconRadio');
        }
        if (fatherList.attr('class').indexOf('f-treeListOne') == -1) {
            t.fatherRadio_false(fatherList)
        }
    }
};
var config = new configFun();